package com.fable.web.controller.operate;

import com.fable.biz.operate.HomeClickService;
import com.fable.biz.operate.enums.VisitEnum;
import com.fable.dal.model.HomeClickDO;
import com.fable.utils.CalendarUtil;
import com.fable.utils.ExcelUtil;
import com.fable.utils.WebContentUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 首页点击报表
 * Created by weishuai on 15/11/30.
 */
public class HomeClickController {
    @Autowired
    HomeClickService homeClickService;

    @Autowired
    protected HttpServletResponse response;

    @RequestMapping("/homeClick.htm")
    public String execute(ModelMap model, @RequestParam(value = "start_date",required = false) String startDateStr,
                          @RequestParam(value = "end_date",required = false) String endDateStr,
                          @RequestParam(value = "export",required = false) String export,
                          @RequestParam(value = "type",required = false) String type,
                          @RequestParam(value = "url",required = false) String url) {
        Date start = null;
        Date end = null;
        //如果开始时间和结束时间为空默认查前一天
        if (StringUtils.isBlank(startDateStr) && StringUtils.isBlank(endDateStr)) {
            String yesterday = CalendarUtil.formatDate(CalendarUtil.addDate(CalendarUtil.getCurrentDate(), -1), CalendarUtil.DATE_FMT_3);
            start = CalendarUtil.toDate(yesterday + " 00:00:00", CalendarUtil.TIME_PATTERN);
            end = CalendarUtil.toDate(yesterday + " 23:59:59", CalendarUtil.TIME_PATTERN);
            startDateStr = CalendarUtil.toString(start, CalendarUtil.DATE_FMT_3);
            endDateStr = startDateStr;
        }

        if (!StringUtils.isBlank(startDateStr) || !StringUtils.isBlank(endDateStr)) {//根据时间段查询
            if (!StringUtils.isBlank(startDateStr) && StringUtils.isBlank(endDateStr)) {
                endDateStr = CalendarUtil.formatDate(CalendarUtil.getCurrentDate(), CalendarUtil.DATE_FMT_3);
            }
            if (!StringUtils.isBlank(startDateStr)) {
                start = CalendarUtil.toDate(startDateStr + " 00:00:00", CalendarUtil.TIME_PATTERN);
            }
            end = CalendarUtil.toDate(endDateStr + " 23:59:59", CalendarUtil.TIME_PATTERN);
            if (CalendarUtil.compareTwoDate(start, end) == CalendarUtil.AFTER) {
                return "";

            }
        }
        List<HomeClickDO> pcList=new ArrayList<HomeClickDO>();
        List<HomeClickDO> h5List = homeClickService.findHomeClickByDate(VisitEnum.H5,url, start, end);

        List<HomeClickDO> prePcList = homeClickService.findHomeClickByDate(VisitEnum.PC, url, start, end);
        //对url中没有标识h5=1或wap的h5的简易活动页面进行是否是h5页面过滤
        for(HomeClickDO homeClickDO:prePcList){
            String clickUrl=homeClickDO.getUrl();
            if(StringUtils.isNotBlank(clickUrl)&&clickUrl.contains("easyAct.htm")&& WebContentUtil.isH5Page(clickUrl)){
                h5List.add(homeClickDO);
            }else{
                pcList.add(homeClickDO);
            }
        }


        model.put("pcLm", pcList);
        model.put("h5Lm", h5List);
        model.put("url", url);
        model.put("startDateStr", startDateStr);
        model.put("endDateStr", endDateStr);
        model.put("allSize", pcList.size()+h5List.size());

        if (!StringUtils.isBlank(export)) {
            List<Map> mapList = new ArrayList<Map>();
            List<List<String>> contentList = new ArrayList<List<String>>();
            getHeadList(contentList);
            if(VisitEnum.PC.name().equals(type)){
                getContentList(pcList, contentList);
            }else if(VisitEnum.H5.name().equals(type)){
                getContentList(h5List, contentList);
            }
            Map m = new HashMap();
            m.put("sheet", "首页点击"+type);
            m.put("list", contentList);
            mapList.add(m);
            ExcelUtil.exportExcel(mapList, "首页点击"+type, response);
        }
        return "";


    }

    private void getContentList(List<HomeClickDO> list, List<List<String>> contentList) {
        for (HomeClickDO homeClickDO : list) {
            List rowContent = new ArrayList();
            rowContent.add(CalendarUtil.toString(homeClickDO.getLogDate(), CalendarUtil.DATE_FMT_3));
            rowContent.add(homeClickDO.getClick());
            rowContent.add(homeClickDO.getUrl());
            contentList.add(rowContent);
        }
    }

    private void getHeadList(List<List<String>> contentList) {
        List<String> headList = new ArrayList<String>();
        headList.add("日期");
        headList.add("点击数");
        headList.add("url");
        contentList.add(headList);
    }

}
